<div class="form_item_form">
	<div id="!{$item.id}_i_nav" class="i_nav">
		<a class="button" id="!{$item.id}_add_btn" href="javascript:void(0)">添加!{$item.name}</a>
	</div>
	<table class="list_table" id="!{$item.id}_table">
		<tr>
		!{foreach from=$list_titles item=name key=field}
			<th>!{$field}</th>
		!{/foreach}
			<th>操作</th>
		</tr>
		!{foreach from=$list_data item=t key=item_id name=attr}
		<tr id="!{$item.id}_item_!{$item_id}" class="item_tr" index="!{$item_id}">
			!{foreach from=$list_titles item=name key=field}
			<td>!{$t.$field}</td>
			!{/foreach}
			<td>
				<a href="javascript:void(0)" onclick="form_item_edit_!{$item.id}('!{$item_id}')"><span class="ui-icon ui-icon-pencil"></span>修改</a> 
				<a href="javascript:void(0)" onclick="form_item_del_!{$item.id}('!{$item_id}')"><span class="ui-icon ui-icon-trash"></span>删除</a>
			</td>
		</tr>
		!{foreachelse}
		<tr id="!{$item.id}_item_null"><td colspan="!{$list_view.title|count + 1}">暂无!{$item.name}</td></tr>
	!{/foreach}
	</table>
</div>
<script>
	var sort_ids = null;
	$(function(){
		$('body').append('<div id="dialog_!{$item.id}" title="!{$item.name}编辑" class="dialog"></div>');
		$("#!{$item.id}_add_btn").click(function(){
			form_item_edit_!{$item.id}();
		});
		sort_ids = init_sort();
	
		$("#items_table").sortable({
			'items' : '.item_tr',
			'helper' : 'clone',
			'stop' : function(){
				if(init_sort() !== sort_ids){
					sort_ids = init_sort();
					var id = $('input[name="id"]').val();
					$.post('/manager.php?app=forms&act=item_sort',{
						'data' : sort_ids,
						'entity_id' : id,
						'lang' : '!{$item.lang}',
						'item_name' : '!{$item.id}',
						'module' : module
					},function(data){
						var json = eval('('+data+')');
						alert(json.info);
					});
				}
			}
		});
		
	});
	
	function init_sort(){
		var ids = '';
		$("#items_table .item_tr").each(function(){
			ids += $(this).attr('index') + ';';
		});
		return ids;
	}

	function form_item_del_!{$item.id}(item_id){
		if(confirm('删除将无法恢复，是否继续？')){
			var id = $('input[name="id"]').val();
			var url = '/index.php?app=form&act=form_item_del&module='+module+'&id='+id+'&item_name=!{$item.id}&item_id=' + item_id + '&lang=!{$item.lang}&src_id=!{$item.src_id}';
			$.get(url,function(data){
				var json = eval('('+data+')');
				alert(json.info);
				if(json.result){
					$("#!{$item.id}_item_" + item_id).remove();
				}
			});
		}
	}
	
	function form_item_edit_!{$item.id}(item_id){
		//此处id/module必须在外面赋值
		var url = '/index.php?app=form&act=form_item_edit&module=' + module + '&form_name=!{$item.forms}&lang=!{$item.lang}&src_id=!{$item.src_id}';
		if(item_id!=null){
			url += '&item_id=' + item_id;
		}
		var id = $('input[name="id"]').val();
		url += '&id=' + id;
		$("#dialog_!{$item.id}").html('正在加载，请稍候...').dialog({width:700,height:420,modal:true,open:function(){
			//打开后加载
			$.get(url,function(html){
				$("#dialog_!{$item.id}").html(html);
				//加载提交
				$('#form_!{$item.forms}').ajax_form({
					'submit_btn' : '.submit_btn',
					'is_alert' : false,
					'success' : function(form, data){
						added_!{$item.id}(data.data);
					}
				});
			});
		}});
	}
	
	function added_!{$item.id}(data){
		$('#!{$item.id}_item_null').remove();
		var data_id = data['_id'];
		$('#!{$item.id}_item_' + data_id).remove();
		var html = "<tr id='!{$item.id}_item_" + data_id + "'>!{foreach from=$list_titles item=name key=field}<td>"+data['!{$field}']+"</td>!{/foreach}";
		html += '<td><a href="javascript:void(0)" onclick="form_item_edit_!{$item.id}(\''+data_id+'\')"><span class="ui-icon ui-icon-pencil"></span>修改</a>' + 
			'<a href="javascript:void(0)" onclick="form_item_del_!{$item.id}(\''+data_id+'\')"><span class="ui-icon ui-icon-trash"></span>删除</a><input type="hidden" name="!{$item.id}[]" value="'+data_id+'"/></td>';
		html += "</tr>";
		$("#!{$item.id}_table").append(html);
		$("#dialog_!{$item.id}").dialog("destroy");
		
	}
</script>